//
//  ZCMeBirthdayVC.m
//  zctx
//
//  Created by apple on 15/12/7.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCMeBirthdayVC.h"
#import "HZQDatePickerView.h"
@interface ZCMeBirthdayVC ()<UITextFieldDelegate,HZQDatePickerViewDelegate>
{
    UITextField *birthdayField;
    HZQDatePickerView *_pikerView;
}
@end

@implementation ZCMeBirthdayVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCMeBirthdayVC"];
    [MTA trackPageViewBegin:@"ZCMeBirthdayVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCMeBirthdayVC"];
    [MTA trackPageViewEnd:@"ZCMeBirthdayVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"生日";
    self.view.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];

    [self initnavegation];
    [self initNameView];
}

-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)initNameView{
    
    UIView *btnView = [[UIView alloc]init];
    [self.view addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(64);
        make.right.equalTo(self.view).with.offset(0);
        make.left.equalTo(self.view).with.offset(0);
        make.height.mas_equalTo(@200);
    }];
    btnView.backgroundColor = [UIColor whiteColor];
    
    UILabel *useLable = [[UILabel alloc]init];
    useLable.text = @"生日：";
    useLable.font = [UIFont systemFontOfSize:16];
    [btnView addSubview:useLable];
    [useLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btnView.top).with.offset(40);
        make.left.equalTo(btnView).with.offset(30);
        make.height.mas_equalTo(@25);
        make.width.mas_equalTo(@70);
    }];
    
    birthdayField = [[UITextField alloc]init];
    [btnView addSubview:birthdayField];
    birthdayField.tag = 1;
    birthdayField.font = [UIFont systemFontOfSize:15];
    [birthdayField setBackgroundColor:[UIColor colorWithHexString:@"f2f2f2"]];
    [birthdayField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(useLable.centerY);
        make.height.mas_equalTo(@(25));
        make.left.equalTo(useLable.right).with.offset(0);
        make.width.mas_equalTo(@(SCREEN_WIDTH/2));
    }];
    birthdayField.delegate = self;
    birthdayField.text = UserModel.birthday;
    
    UIButton *confirmButton = [UIButton new];
    [btnView addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(birthdayField.bottom).with.offset(30);
        make.right.equalTo(btnView).with.offset(-30);
        make.left.equalTo(btnView).with.offset(30);
        make.height.mas_equalTo(@44);
    }];
    [confirmButton setTitle:@"确认" forState:UIControlStateNormal];
    confirmButton.backgroundColor = MainColor;
    confirmButton.layer.cornerRadius =4;[confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 点击确认
-(void)confirmButtonClick{
    if (birthdayField.text<=0) {
        [SVProgressHUD showInfoWithStatus:@"生日不能为空"];
        return;
    }
    [NetworkManager requestModifybirthdayWithMid:UserModel.mid birthday:birthdayField.text success:^(NSDictionary *ArrayResponse) {
        NSString *err = [NSString stringWithFormat:@"%@",[ArrayResponse objectForKey:@"err" ]];  // 1 失败 0  成功
        if ([err isEqualToString:@"0"]) {
            NSLog(@"成功");
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            [UserModel setBirthday:birthdayField.text];
              [[NSNotificationCenter defaultCenter]postNotificationName:@"userinfo" object:nil userInfo:@{@"user":@"3"}];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            NSLog(@"操作失败");
            NSString *msg = [NSString stringWithFormat:@"%@",[ArrayResponse objectForKey:@"msg" ]];
            [SVProgressHUD showErrorWithStatus:msg];
        }
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark --- UITextFieldDelegate

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    
    _pikerView = [[HZQDatePickerView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, 260)];
    _pikerView.delegate = self;
    [_pikerView.datePickerView setMaximumDate:[NSDate date]];
    [_pikerView show];
    
}

- (void)getSelectDate:(NSString *)date type:(DateType)type {
    NSLog(@"生日 - %@", date);
    birthdayField.text = date;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
